package test.controller;

import jdbc.InputUtil;
import test.mapper.ArticleMapper;
import test.vo.Article;
import java.util.List;
import java.util.Scanner;

public class ArticleController {
    private ArticleMapper am=new ArticleMapper();
    public static void main(String[] args) {
        ArticleController articleController=new ArticleController();
        articleController.start();
    }
    public void start(){
        a:while (true){
            System.out.println("欢迎来到文章管理系统");
            System.out.println("请输入操作编号");
            System.out.println("1,保存文章");
            System.out.println("2,删除文章");
            System.out.println("3,查看指定文章");
            System.out.println("4,查询说有文章");
            System.out.println("5,退出查询");
        Scanner scan=new Scanner(System.in);
        int num=scan.nextInt();
        switch(num){
            case 1:
                writeArticle();
                System.out.println("保存文章操作");
                break;
            case 2:
                removeArticle();
                System.out.println("删除文章操作");
                break;
            case 3:
                findArticle();
                System.out.println("查看文章操作");
                break;
            case 4:
                findALLArticle();
                System.out.println("查看所有文章");
                break;
            case 5:
                System.out.println("再见！");
                break a;
            default:
                System.out.println("编号输入有误");
        }}
    }
    public  void writeArticle(){
        Article article=InputUtil.getInputObject(new Article(),"发表文章");
        int num=am.insertArticle(article);
        if (num>0){
            System.out.println("插入成功");
        }
        else {
            System.out.println("插入失败");
        }
    }
    public void removeArticle(){
        System.out.println("删除文章");
        System.out.println("请输入要删除的文章id");
        Scanner scanner = new Scanner(System.in);
        int id = Integer.parseInt(scanner.nextLine());
        int num = am.deleteArticleById(id);
        System.out.println(num>0?"删除成功":"删除失败");
    }
    public void findArticle(){
        System.out.println("查找文章");
        System.out.println("请输入你要查找文章的编号");
        Scanner scan = new Scanner(System.in);
        int id = Integer.parseInt(scan.nextLine());
        Article article=am.findById(id);
        System.out.println("标题:"+article.getTitle());
        System.out.println("内容:"+article.getContent());

    }
    public void findALLArticle() {
        System.out.println("查看所有文章");
        List<Article> list=am.findAll();
        int a=1;
        for(Article article:list){
            System.out.println(a+++"、");
            System.out.println("标题:"+article.getTitle());
            System.out.println("内容:"+article.getContent());
        }
    }
}
